package be.ibridge.kettle.kgeocode;

import java.util.List;
import java.util.Map;

import org.eclipse.swt.widgets.Shell;
import org.pentaho.di.core.CheckResult;
import org.pentaho.di.core.CheckResultInterface;
import org.pentaho.di.core.Counter;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleDatabaseException;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettleXMLException;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.core.row.ValueMeta;
import org.pentaho.di.core.row.ValueMetaInterface;
import org.pentaho.di.core.variables.VariableSpace;
import org.pentaho.di.core.xml.XMLHandler;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.step.BaseStepMeta;
import org.pentaho.di.trans.step.StepDataInterface;
import org.pentaho.di.trans.step.StepDialogInterface;
import org.pentaho.di.trans.step.StepInterface;
import org.pentaho.di.trans.step.StepMeta;
import org.pentaho.di.trans.step.StepMetaInterface;
import org.w3c.dom.Node;

/*
 * Created on 02-jun-2003
 *
 */

public class KGeoCodingPluginMeta extends BaseStepMeta implements StepMetaInterface
{
	//private ValueMetaAndData value;
	
	private String GMapKey;
	private String AddressField;
	private String NormalizedAddress;
	private String City;
	private String GPSCoord;
	
	
	public KGeoCodingPluginMeta()
	{
		super(); // allocate BaseStepInfo
	}
		
	// Column
	public String getGMapKey() {
		return GMapKey;
	}
	public void setGMapKey(String GMapKey) {
		this.GMapKey = GMapKey;
	}
	// SizeX
	public String getAddressField() {
		return AddressField;
	}
	public void setAddressField(String AddressField) {
		this.AddressField = AddressField;
	}
	// SizeY
	public String getNormalizedAddress() {
		return NormalizedAddress;
	}
	public void setNormalizedAddress(String NormalizedAddress) {
		this.NormalizedAddress = NormalizedAddress;
	}
	// Pic format
	public String getCity() {
		return City;
	}
	public void setCity(String City) {
		this.City = City;
	}
	// DestDir
	public String getGPSCoord() {
		return GPSCoord;
	}
	public void setGPSCoord(String GPSCoord) {
		this.GPSCoord = GPSCoord;
	}
	
	
	/**
	 * @return Returns the value.
	 */
	/*
	public ValueMetaAndData getValue()
	{
		return value;
	}
	*/
	/**
	 * @param value The value to set.
	 */
	/*
	public void setValue(ValueMetaAndData value)
	{
		this.value = value;
	}
	*/
	/////////////////////////////////////////////////////////////
	public String getXML()
	{
        final StringBuilder retval = new StringBuilder(1000);

        retval.append("   ").append(XMLHandler.addTagValue("GMapKey", GMapKey)); //$NON-NLS-1$ //$NON-NLS-2$
        retval.append("   ").append(XMLHandler.addTagValue("AddressField", AddressField)); //$NON-NLS-1$ //$NON-NLS-2$
        retval.append("   ").append(XMLHandler.addTagValue("NormalizedAddress", NormalizedAddress)); //$NON-NLS-1$ //$NON-NLS-2$
        retval.append("   ").append(XMLHandler.addTagValue("City", City)); //$NON-NLS-1$ //$NON-NLS-2$
        retval.append("   ").append(XMLHandler.addTagValue("GPSCoord", GPSCoord)); //$NON-NLS-1$ //$NON-NLS-2$
        
        return retval.toString();
	}

	public void getFields(RowMetaInterface r, String origin, RowMetaInterface[] info, StepMeta nextStep, VariableSpace space)
	{
		//With that function, field metadata is available in next steps
		/*if (value!=null)
		{
			ValueMetaInterface v = value.getValueMeta();
			v.setOrigin(origin);
			
			r.addValueMeta( v );
			*/
		
		ValueMetaInterface v = new ValueMeta(getNormalizedAddress().toString(), ValueMeta.TYPE_STRING, 80, 0);
		v.setOrigin(origin);
		r.addValueMeta(v);
		
		v = new ValueMeta(getCity().toString(), ValueMeta.TYPE_STRING, 44, 0);
		v.setOrigin(origin);
		r.addValueMeta(v);
		
		v = new ValueMeta(getGPSCoord().toString(), ValueMeta.TYPE_STRING, 44, 0);
		v.setOrigin(origin);
		r.addValueMeta(v);
	}

	public Object clone()
	{
		Object retval = super.clone();
		return retval;
	}

	public void loadXML(Node stepnode, List<DatabaseMeta> databases, Map<String,Counter> counters)
		throws KettleXMLException
	{
		try
		{	
			setGMapKey(XMLHandler.getTagValue(stepnode, "GMapKey"));
			setAddressField(XMLHandler.getTagValue(stepnode, "AddressField"));
			setNormalizedAddress(XMLHandler.getTagValue(stepnode, "NormalizedAddress"));
			setCity(XMLHandler.getTagValue(stepnode, "City"));
			setGPSCoord(XMLHandler.getTagValue(stepnode, "GPSCoord"));
		}
		catch(Exception e)
		{
			throw new KettleXMLException("Unable to read step info from XML node", e);
		}
	}

	public void setDefault()
	{
		/*QRColumn = "";
		QRSizeX = "128";
		QRSizeY = "128"; */
	}

	public void readRep(Repository rep, long id_step, List<DatabaseMeta> databases, Map<String,Counter> counters) throws KettleException
	{
		try
		{
			setGMapKey(rep.getStepAttributeString(id_step,0, "GMapKey"));
			setAddressField(rep.getStepAttributeString(id_step, "AddressField"));
			setNormalizedAddress(rep.getStepAttributeString(id_step, "NormalizedAddress"));
			setCity(rep.getStepAttributeString(id_step, "City"));
			setGPSCoord(rep.getStepAttributeString(id_step, "GPSCoord"));
		}
		catch(KettleDatabaseException dbe)
		{
			throw new KettleException("error reading step with id_step="+id_step+" from the repository", dbe);
		}
		catch(Exception e)
		{
			throw new KettleException("Unexpected error reading step with id_step="+id_step+" from the repository", e);
		}
	}
	
	public void saveRep(Repository rep, long id_transformation, long id_step) throws KettleException
	{
		try
		{
			rep.saveStepAttribute(id_transformation, id_step, "GMapKey", getGMapKey());
			rep.saveStepAttribute(id_transformation, id_step, "AddressField", getAddressField());
			rep.saveStepAttribute(id_transformation, id_step, "NormalizedAddress", getNormalizedAddress());
			rep.saveStepAttribute(id_transformation, id_step, "City", getCity());
			rep.saveStepAttribute(id_transformation, id_step, "GPSCoord", getGPSCoord());
		}
		catch(KettleDatabaseException dbe)
		{
			throw new KettleException("Unable to save step information to the repository, id_step="+id_step, dbe);
		}
	}

	public void check(List<CheckResultInterface> remarks, TransMeta transmeta, StepMeta stepMeta, RowMetaInterface prev, String input[], String output[], RowMetaInterface info)
	{
		CheckResult cr;
		if (prev==null || prev.size()==0)
		{
			cr = new CheckResult(CheckResult.TYPE_RESULT_WARNING, "Not receiving any fields from previous steps!", stepMeta);
			remarks.add(cr);
		}
		else
		{
			cr = new CheckResult(CheckResult.TYPE_RESULT_OK, "Step is connected to previous one, receiving "+prev.size()+" fields", stepMeta);
			remarks.add(cr);
		}
		
		// See if we have input streams leading to this step!
		if (input.length>0)
		{
			cr = new CheckResult(CheckResult.TYPE_RESULT_OK, "Step is receiving info from other steps.", stepMeta);
			remarks.add(cr);
		}
		else
		{
			cr = new CheckResult(CheckResult.TYPE_RESULT_ERROR, "No input received from other steps!", stepMeta);
			remarks.add(cr);
		}
	}
	
	public StepDialogInterface getDialog(Shell shell, StepMetaInterface meta, TransMeta transMeta, String name)
	{
		return new KGeoCodingPluginDialog(shell, meta, transMeta, name);
	}

	public StepInterface getStep(StepMeta stepMeta, StepDataInterface stepDataInterface, int cnr, TransMeta transMeta, Trans disp)
	{
		return new KGeoCodingPlugin(stepMeta, stepDataInterface, cnr, transMeta, disp);
	}

	public StepDataInterface getStepData()
	{
		return new KGeoCodingPluginData();
	}
}
